#include <iostream>
#include <pthread.h>

using std::cout;
using std::endl;

#define N 10000000

struct Temp{
    int num;
    pthread_mutex_t mutex;
};

void *func(void *p)
{
    struct Temp *tmp = (struct Temp *)p;

    for(int i{}; i < N; i++){
        pthread_mutex_lock(&tmp->mutex);
        tmp->num ++;
        pthread_mutex_unlock(&tmp->mutex);
    }
}

int main(int args, char *argv[])
{
    struct Temp tmp;    
    tmp.num = 0;
    pthread_mutex_init(&tmp.mutex, nullptr);

    pthread_t thid;
    pthread_create(&thid, nullptr, func, &tmp);

    for(int i{}; i < N; i++){
        pthread_mutex_lock(&tmp.mutex);
        tmp.num ++;
        pthread_mutex_unlock(&tmp.mutex);
    }
    pthread_join(thid, nullptr);

    printf("num = %d\n", tmp.num);
    return 0;
}